﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CMN.Controllers.Filters;
namespace CMN.Controllers
{
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class Sys_UserModuleController : Controller
    {
        
        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            return View("Index");//页面地址
        }

        /// <summary>
        /// 返回用户常用功能列表
        /// </summary>
        /// <returns>json</returns>
        public string List()
        {
            //返回json数组
          
            int pageSize = (int)Utils.GetQueryAndFormString("limit", this, 4, 1, DataType.Int);
            int pageIndex = (int)Utils.GetQueryAndFormString("start", this, 9, 1, DataType.Int);
            QueryParam qp = new QueryParam();
            qp.PageIndex = pageIndex / pageSize + 1; ;
            qp.PageSize = pageSize;
            qp.Where = "userId=" + Framework.UserData.GetUserData.UserID.ToString();
            int recordNum = 0;
            IsoDateTimeConverter jsDataConvert= new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            jsDataConvert.DateTimeFormat = "yyyy-MM-dd";
            IDataService<Sys_UserModuleEntity> Sys_UserModuleSv = ServiceBuilder.BuildSys_UserModuleService();
            List<Sys_UserModuleEntity> sys_usermoduleList = Sys_UserModuleSv.List(qp, out recordNum);
            string moduleIdList = string.Empty;
            for(int i=0;i<sys_usermoduleList.Count;i++)
            {
                if (i == 0)
                {
                    moduleIdList = sys_usermoduleList[i].ModuleId.ToString();
                }
                else
                {
                    moduleIdList = string.Concat(moduleIdList, ",", sys_usermoduleList[i].ModuleId.ToString());
                }
            }
            IDataService<Sys_ModuleEntity> ModuleSv=ServiceBuilder.BuildSys_ModuleService();
            qp.PageIndex=1;
            qp.Where=string.Concat(" ModuleID in (",moduleIdList,")");
            qp.ReturnFields = "*";
            qp.Orderfld = "";
            qp.NotInRowName = "";
            List<Sys_ModuleEntity> moduleList=ModuleSv.List(qp,out recordNum);
            for(int j=0;j<moduleList.Count;j++){

                for (int i = 0; i < sys_usermoduleList.Count; i++)
                {
                    if (moduleList[j].ModuleID == sys_usermoduleList[i].ModuleId)
                    {
                        sys_usermoduleList[i].ModuleName = moduleList[j].M_CName;
                        break;
                    }
                   
                }
            }
            returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(sys_usermoduleList, Formatting.Indented, jsDataConvert), "}");
            return returnJson;
 
        }

        /// <summary>
        /// 查看用户常用功能 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Look(string actionId)
        {
           IDataService<Sys_UserModuleEntity> Sys_UserModuleSv = ServiceBuilder.BuildSys_UserModuleService();
           int intId = Utils.StringToInt(actionId, 0);
            Sys_UserModuleEntity sys_usermodule = Sys_UserModuleSv.Look(intId);
            if (sys_usermodule != null)
                returnJson = string.Concat("{success:true,data:", JsonConvert.SerializeObject(sys_usermodule), "}");
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 更新用户常用功能
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Update(string actionId)
        {
            int intId = Utils.StringToInt(actionId, 0);
            if (intId != 0)
            {

                Sys_UserModuleEntity sys_usermodule = new Sys_UserModuleEntity();
                sys_usermodule.ModuleId = (Int32)Utils.GetQueryAndFormString("ModuleId", this, 10, 1, DataType.Int);

                IDataService<Sys_UserModuleEntity> Sys_UserModuleSv = ServiceBuilder.BuildSys_UserModuleService();


                sys_usermodule.DataTable_Action = DataTableAction.Update;
                sys_usermodule.Id = intId;
                sys_usermodule.UserId = Framework.UserData.GetUserData.UserID;

                sys_usermodule.OrderNum = (Int32)Utils.GetQueryAndFormString("OrderNum", this, 10, 0, DataType.Int);

                if (Sys_UserModuleSv.Update(sys_usermodule) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";

            }
            else
            {
                returnJson = "{success:'false'}";
            }

            return returnJson;
        }

        /// <summary>
        /// 新增用户常用功能
        /// </summary>
        /// <returns>json</returns>
        public string Insert()
        {
            Sys_UserModuleEntity sys_usermodule = new Sys_UserModuleEntity();
            sys_usermodule.DataTable_Action = DataTableAction.Insert;
            sys_usermodule.UserId = Framework.UserData.GetUserData.UserID;
            sys_usermodule.ModuleId = (Int32)Utils.GetQueryAndFormString("ModuleId", this, 10, 1, DataType.Int);
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.Where = string.Concat("userId=", Framework.UserData.GetUserData.UserID.ToString(), " and moduleId=", sys_usermodule.ModuleId);
            int recordNum = 0;
            IDataService<Sys_UserModuleEntity> Sys_UserModuleSv = ServiceBuilder.BuildSys_UserModuleService();
            List<Sys_UserModuleEntity> sys_usermoduleList = Sys_UserModuleSv.List(qp, out recordNum);
            if (recordNum > 0)
            {
                returnJson = "{success:'false',message:'已经添加过该应用！'}";
            }
            else
            {
                sys_usermodule.OrderNum = (Int32)Utils.GetQueryAndFormString("OrderNum", this, 10, 0, DataType.Int);

                if (Sys_UserModuleSv.Insert(sys_usermodule) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            return returnJson;
        }

        /// <summary>
        /// 删除用户常用功能
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Delete(string actionId)
        {
            if (string.IsNullOrEmpty(actionId))
            {
                Sys_UserModuleEntity sys_usermodule = new Sys_UserModuleEntity();
                sys_usermodule.DataTable_Action = DataTableAction.Insert;
                //sys_usermodule.UserId = Framework.UserData.GetUserData.UserID;
                sys_usermodule.ModuleId = (Int32)Utils.GetQueryAndFormString("ModuleId", this, 10, 1, DataType.Int);
                QueryParam qp = new QueryParam();
                qp.PageIndex = 1;
                qp.PageSize = 1;
                qp.Where = string.Concat("userId=", Framework.UserData.GetUserData.UserID.ToString(), " and moduleId=", sys_usermodule.ModuleId);
                int recordNum = 0;
                IDataService<Sys_UserModuleEntity> Sys_UserModuleSv = ServiceBuilder.BuildSys_UserModuleService();
                List<Sys_UserModuleEntity> sys_usermoduleList = Sys_UserModuleSv.List(qp, out recordNum);
                if (recordNum > 0)
                {

                    sys_usermodule.Id = sys_usermoduleList[0].Id;
                    if (Sys_UserModuleSv.Delete(sys_usermodule) == "")
                    {
                        returnJson = "{success:'true'}";
                    }
                    else
                        returnJson = "{success:'false','删除失败！'}";
                }
                else
                {
                    returnJson = "{success:'false','该用户没有添加该应用！'}";
                }
            }
            else
            {
                int intId = Utils.StringToInt(actionId, 0);
                if (intId != 0)
                {
                    Sys_UserModuleEntity sys_usermodule = new Sys_UserModuleEntity();
                    sys_usermodule.Id = intId;
                    IDataService<Sys_UserModuleEntity> Sys_UserModuleSv = ServiceBuilder.BuildSys_UserModuleService();
                    if (Sys_UserModuleSv.Delete(sys_usermodule) == "")
                    {
                        returnJson = "{success:'true'}";
                    }
                    else
                        returnJson = "{success:'false'}";
                }
                else
                {
                    returnJson = "{success:'false'}";
                }
            }
            return returnJson;
        }

        /// <summary>
        /// 批量删除用户常用功能
        /// </summary>
        /// <param name="idList">id列表 234_4_5</param>
        /// <returns>json</returns>
        public string DeleteList(string id)
        {
            Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            if (regex.IsMatch(id))
            {

                IDataService<Sys_UserModuleEntity> Sys_UserModuleSv = ServiceBuilder.BuildSys_UserModuleService();
                if (Sys_UserModuleSv.DeleteList(id) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";

            }
            else
            {
                returnJson = "{success:'false',message:'参数类型不正确'}";
            }
            return returnJson;
        }

    }
}
